package swings.mycomponent;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * @Title: TrayFactory.java
 * @Description: TODO(系统托盘实现. 本实例仅限于 frame 操作)
 * @author H819
 * @date 2010-12-19
 * @version V1.0
 */

public class SystemTrayFactory {

	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("swing.swingmessages");

	private TrayIcon trayIcon = null;
	private Window comp = null;
	SystemTray systemTray = null;
	private String trayImageName = null;
	private String tooltip = null;

	/**
	 * 私有方法，通过静态方法调用。
	 * 
	 * @param windowsCompnent
	 *            准备应用 systemTray 的组件,必须为 Window 类型，否则需要修改实现代码
	 * @param trayImagePath
	 *            icon 的路径
	 * @param tooltip
	 *            systemTray tips
	 * @throws Exception
	 */
	private SystemTrayFactory(Window windowsCompnent, String trayImagePath,
			String tooltip) throws Exception {
		this.comp = windowsCompnent;
		this.trayImageName = trayImagePath;
		this.tooltip = tooltip;

		initialize();
	}

	private void initialize() throws Exception {

		if (SystemTray.isSupported()) {

			/**
			 * 点击窗口最小化按钮时，隐藏窗口，模拟窗口最小化到托盘
			 */
			comp.addWindowListener(new WindowAdapter() {
				@Override
				public void windowIconified(WindowEvent arg0) {
					// comp.setVisible(false);
					comp.dispose();
					System.out.println("windowIconified");
				}
			});

			/**
			 * 创建 maxMenu 菜单项
			 */
			MenuItem maxMenu = new MenuItem(BUNDLE.getString("systemTray.max"));
			ActionListener maxListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("maxMenu was clicked...");
					comp.setVisible(true);
				}
			};

			maxMenu.addActionListener(maxListener);

			/**
			 * 创建 minMenu 菜单项
			 */
			MenuItem minMenu = new MenuItem(BUNDLE.getString("systemTray.min"));
			ActionListener minListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("minMenu was clicked...");
					comp.dispose();
				}
			};

			minMenu.addActionListener(minListener);

			/**
			 * 创建 aboutMenu 菜单项
			 */
			MenuItem aboutMenu = new MenuItem(
					BUNDLE.getString("systemTray.about"));
			ActionListener aboutListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("aboutMenu was clicked...");

					JOptionPane
							.showMessageDialog(
									comp,
									BUNDLE.getString("menu.about.info"),
									"About",
									JOptionPane.INFORMATION_MESSAGE,
									new ImageIcon(
											SystemTrayFactory.class
													.getResource("/swing/source/image/java-duke-logo_80x80.png")));

				}
			};
			aboutMenu.addActionListener(aboutListener);

			/**
			 * 创建 exitMenu 菜单项
			 */
			MenuItem exitMenu = new MenuItem(
					BUNDLE.getString("systemTray.exit"));
			ActionListener exitListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("exitMenu was clicked...");
					System.exit(0);
				}
			};
			exitMenu.addActionListener(exitListener);

			// 创建快捷菜单，加入两个菜单项
			PopupMenu popup = new PopupMenu();
			popup.add(maxMenu);
			popup.add(minMenu);
			popup.add(aboutMenu);
			popup.add(exitMenu);

			// 创建托盘图标
			InputStream in = getClass().getResourceAsStream(trayImageName);
			byte buffer[] = new byte[in.available()];
			for (int i = 0, n = in.available(); i < n; i++)
				buffer[i] = (byte) in.read();
			Image image = Toolkit.getDefaultToolkit().createImage(buffer);

			trayIcon = new TrayIcon(image, tooltip, popup);

			trayIcon.setImageAutoSize(true);

			MouseListener mouseListener = new MouseListener() {
				// 鼠标点击事件
				public void mouseClicked(MouseEvent e) {
					System.out.println("TrayIcon - Mouse Clicked. "
							+ e.getClickCount() + " times ");
					if (e.getClickCount() == 2) {
						comp.setVisible(true);
						comp.toFront();
					}
				}

				public void mouseEntered(MouseEvent e) {
					System.out.println("TrayIcon - Mouse Entered");
				}

				public void mouseExited(MouseEvent e) {
					System.out.println("TrayIcon - Mouse Exited");
				}

				public void mousePressed(MouseEvent e) {
					System.out.println("TrayIcon - Mouse Pressed");
				}

				public void mouseReleased(MouseEvent e) {
					System.out.println("TrayIcon - Mouse Released");
				}
			};

			trayIcon.addMouseListener(mouseListener);

			// 创建系统托盘，加入托盘图标
			systemTray = SystemTray.getSystemTray();
			systemTray.add(trayIcon);

		}
	}

	/*
	 * private class TrayIconActionListener implements ActionListener { public
	 * void actionPerformed(ActionEvent ae) { comp.setVisible(true); } }
	 */
	public static void createTray(Window windowscom, String trayImageName,
			String tooltip) {
		try {
			new SystemTrayFactory(windowscom, trayImageName, tooltip);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
